﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SQLExpressDAL;

public partial class Manageweb_Stock_StockManage : System.Web.UI.Page
{

    SQLExpressDAL.StockTableAdapters.StockTableAdapter adp = new SQLExpressDAL.StockTableAdapters.StockTableAdapter();
    SQLExpressDAL.StockTableAdapters.StockUpdateTableAdapter adpUpdate = new SQLExpressDAL.StockTableAdapters.StockUpdateTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {

        InitEvent();

        if (!Page.IsPostBack)
        {
            InitPage();
        }

    }

    protected void InitEvent()
    {

        switch (this.hidOperateType.Value)
        {
            case "add":
                this.Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                break;
            case "zhuijia":
                this.Btn_Submit.Click += new EventHandler(Btn_Update_Click);
                break;

        }



    }

    protected void Btn_Add_Click(object sender, EventArgs e)
    {

        try
        {
            String stockCode = this.txtStockCode.Text.Trim();
            String stockName = this.txtStockName.Text.Trim();
            Decimal stockPrice = Convert.ToDecimal(this.txtStockPrice.Text.Trim());
            Int32 stockCount = Convert.ToInt32(this.txtStockCount.Text.Trim());

            if (adp.CheckStockIsExist(stockCode) > 0)
            {
                throw new Exception(" 已存在相同的股票代码!请更换. ");
            }

            //adp.Insert(stockCode, stockName, stockPrice, DateTime.Now, stockPrice.ToString(), "Y",stockCount);

            //adpUpdate.Insert(stockCode, stockPrice, DateTime.Now);

            string IID = System.Guid.NewGuid().ToString();

            DealRecord obj = new DealRecord();
            int flag = obj.AddStock(IID, stockPrice, stockCount, stockName, stockCode);

            if (flag == 0)
            {
                this.LB_ManageUser.Text = "添加成功";
            }
            else
            {
                this.LB_ManageUser.Text = "添加失败";
            }


            this.GV_UserList.DataBind();
        }
        catch (Exception ex)
        {
            this.LB_ManageUser.Text = ex.Message;
        }

        ClearText();
        this.TABLE_Add_Mod.Visible = false;
    }

    protected void Btn_Update_Click(object sender, EventArgs e)
    {
        String msg = String.Empty;
        try
        {
            String stockCode = this.hidStockCode.Value;
            String stockName = this.txtStockName.Text.Trim();
            Decimal stockPrice = Convert.ToDecimal(this.txtStockPrice.Text.Trim());
            Int32 stockID = Convert.ToInt32(this.hidStockNum.Value);
            Int32 stockCount = Convert.ToInt32(this.txtStockCount.Text.Trim());

            SQLExpressDAL.DealRuleTableAdapters.DealRecordTableAdapter dealRecordAdp = new SQLExpressDAL.DealRuleTableAdapters.DealRecordTableAdapter();
            int isSuccess = dealRecordAdp.InsertDealRecord(System.Guid.NewGuid(), stockID.ToString(), stockCount, stockPrice);


            if (isSuccess > 0)
            {
                msg = "发行成功!";
            }
            else
            {
                throw new Exception("发行失败!");
            }
 
            adpUpdate.Insert(stockCode, stockPrice, DateTime.Now,this.txtStockCount.Text);
          



        }
        catch (Exception ex)
        {
            msg = ex.Message;

        }

        ClearText();

        this.TABLE_Add_Mod.Visible = false;
        this.GV_UserList.DataBind();
        this.LB_ManageUser.Text = msg;
    }

    protected void InitPage()
    {

        int recordCount = (int)adp.CheckHasData();

        if (recordCount == 0)
        {
            this.TABLE_Add_Mod.Visible = true;
        }
    }

    #region Event

    protected void LBtn_Add_Click(object sender, EventArgs e)
    {
        this.TABLE_Add_Mod.Visible = true;
        this.txtStockCode.ReadOnly = false;
        this.LB_TableTitle.Text = "添加股票";
        this.LB_ManageUser.Text = "";
        this.hidOperateType.Value = "add";
        ClearText();
    }

    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        String[] stockInfo = e.CommandArgument.ToString().Split(new char[] { '|' });

        this.hidStockCode.Value = stockInfo[0];
        this.txtStockCode.Text = stockInfo[0];
        this.txtStockName.Text = stockInfo[1];
        this.txtStockPrice.Text = stockInfo[2];
        this.hidStockNum.Value = stockInfo[3];
        this.txtStockCount.Text = stockInfo[4];

        this.txtStockCode.ReadOnly = true;
        this.txtStockCount.ReadOnly = true;
        this.txtStockPrice.ReadOnly = true;
        this.TABLE_Add_Mod.Visible = true;
        this.LB_TableTitle.Text = "修改股票";

    }
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {

        String msg = String.Empty;

        try
        {
            String stockCode = e.CommandArgument.ToString();

            int isSuccess = adp.UpdateStockDataState(stockCode);

            if (isSuccess > 0)
            {
                msg = "删除成功!";
            }
        }
        catch (Exception ex)
        {
            msg = ex.Message;

        }
        this.GV_UserList.DataBind();
        this.LB_ManageUser.Text = msg;

    }

    protected void GV_UserList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_UserList.PageIndex = e.NewPageIndex;
        this.GV_UserList.DataBind();
    }


    #endregion

    #region Method

    protected void ClearText()
    {
        this.hidStockCode.Value = "";
        this.txtStockCode.Text = "";
        this.txtStockName.Text = "";
        this.txtStockPrice.Text = "";
        this.hidStockNum.Value = "";
        this.txtStockCount.Text = "";
    }
    #endregion

    protected void lbtnAddCount_Command(object sender, CommandEventArgs e)
    {
        String[] stockInfo = e.CommandArgument.ToString().Split(new char[] { '|' });

        this.hidStockCode.Value = stockInfo[0];
        this.txtStockCode.Text = stockInfo[0];
        this.txtStockName.Text = stockInfo[1];
        this.txtStockPrice.Text = stockInfo[2];
        this.hidStockNum.Value = stockInfo[3];
        this.txtStockCount.Text = stockInfo[4];

        this.txtStockCode.ReadOnly = true;
        this.txtStockCount.ReadOnly = false;
        this.txtStockPrice.ReadOnly = false;
        this.TABLE_Add_Mod.Visible = true;
        this.LB_TableTitle.Text = "增加发行量";

        this.hidOperateType.Value = "zhuijia";
    }
   
}
